package jzqb.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import haha.life.common.BelongToSqlUtils;
import haha.life.common.CutPageBean;
import haha.life.common.SysTool;
import haha.life.normal.FileUtil;
import haha.life.normal.FileuploadUtils;
import jzqb.bo.*;
import jzqb.common.Result;
import jzqb.dao.ResultDao;
import jzqb.service.ResultService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author 关注公众号：MarkerHub
 * @since 2020-09-22
 */
@Service
public class ResultServiceImpl implements ResultService {

    @Value("${filePath.qbcgCoverPath}")
    private String qbcgCoverPath;

    @Value("${filePath.qbzyCoverPath}")
    private String qbzyCoverPath;

    @Autowired
    ResultDao resultDao;

    @Override
    public CutPageBean<StandardBo> ComplexSearch(ResultBo resultBo) {
        CutPageBean<StandardBo> cutPageBean = new CutPageBean<>();
        //搜索的领域来源
        JSONArray areaArr = (JSONArray) JSON.parse(resultBo.getSearchAreaResource());
        if (areaArr != null  && areaArr.size()>0){
            List<String> areaString = JSONObject.parseArray(areaArr.toJSONString(),String.class);
            String[] areaStr = areaString.toArray(new String[areaString.size()]);
             String  belongSql = BelongToSqlUtils.getBelongToSql(areaStr,"领域");
             resultBo.setAreaBelongSql(belongSql);
        }
        resultBo.setStart((resultBo.getPage() - 1) * resultBo.getPageSize());
        List<StandardBo> standardBos = resultDao.ComplexSearch(resultBo);
        String absolutePath = "";
        try {
            for (StandardBo standardBo : standardBos) {
                String cover =standardBo.getCoverPage();
                if("".equals(cover) || cover == null){
                    standardBo.setCoverByte(null);
                }else{
                    if (SysTool.QBCG_METADATA.equals(standardBo.getTableName())){
                        absolutePath = qbcgCoverPath;
                    }else {
                        absolutePath = qbzyCoverPath;
                    }
                    String path = FileUtil.calculateAbstractPath(cover,absolutePath);
                    standardBo.setCoverPath(path);
                }
            }
        }catch (Exception e){
         // e.printStackTrace();
        }
        Long count = resultDao.findComplexSearchCount(resultBo);
        cutPageBean.initCutPage(count.intValue(), resultBo.getPageSize(), standardBos);
        return cutPageBean;
    }

    @Override
    public Result getResultDetail(ResultBo  resultBo) {
        CGBo cgBoo = resultDao.getResultDetail(resultBo);
        String cover =cgBoo.getCover();
        try {
            if("".equals(cover) || cover == null){
                cgBoo.setCoverByte(null);
            }else{
                String path = FileUtil.calculateAbstractPath(cover,qbcgCoverPath);
                if (!new File(path).exists() ) {
                    cgBoo.setCoverByte(null);
                } else {
                    byte[] imageBinary = FileuploadUtils.getImageBinary(path);
                    cgBoo.setCoverByte(imageBinary);
                }
            }

        }catch (Exception e){
        //    e.printStackTrace();
        }
        return Result.succ(cgBoo);
    }
}